#pragma once
#include "IPALDataStreamBuffer.h"



class CMemoryStreamBuffer :
	public IPALDataStreamBuffer
{
public: 
	HRESULT Read(void * pv, XUINT32 cb, XUINT32 * pcbRead);
	HRESULT Init();
	XUINT32 Size();
	virtual XUINT32 AddRef();
	virtual XUINT32 Release();
	virtual HRESULT Write(const void * pData, XUINT32 cb, XINT32 cbOffset);
	virtual HRESULT CreateStream(struct IPALStream * * ppStream);
	CMemoryStreamBuffer(XUINT32 cb);
	virtual ~CMemoryStreamBuffer();
	class CMemoryStreamBufferBlock
	{
	public: 
		CMemoryStreamBufferBlock();
		~CMemoryStreamBufferBlock();
		HRESULT Write(XUINT32 cb, const void * pData);
		HRESULT Read(void * pv, XUINT32, XUINT32, XUINT32 * pcbRead);
		void SetNext(CMemoryStreamBufferBlock * pNext);
		CMemoryStreamBufferBlock * GetNext();
		XUINT32 GetSize();
	private: 
		UINT8 * m_pBlock;
		XUINT32 m_uSize;
		CMemoryStreamBufferBlock * m_pNext;
	};

private: 
	XUINT32 m_uCurrentBlockOffset;
	CMemoryStreamBufferBlock * m_pBlockFirst;
	CMemoryStreamBufferBlock * m_pBlockLast;
	struct IPALCriticalSection * m_pLock;
	XUINT32 m_cRef;
	XUINT32 m_uSize;
	XUINT32 m_bUpdateSize;
};

